define(function(require, exports, module)
{
    var $ = require("$");
    var bootbox = require("bootbox");
    exports.init = function(param)
    {
        var Table = require("table");
        new Table(
        {
            attrs :
            {
                id : "menutable",
                isHovered : false,
                isStriped : true,
                isBordered : false,
            },
            renderTo : param.divId,
        });

        $(document).ready(function()
        {
            var menutable = $('#menutable').dataTable(
            {
                "sDom" : "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
                "bJQueryUI" : false,
                "oLanguage" :
                {
                    "sProcessing" : "正在加载数据...",
                    "sSearch" : "搜索",
                    "sZeroRecords" : "没有符合条件的记录！",
                },
                "sPaginationType" : "bootstrap", // 分页自带两种样式，full_numbers和two_button(默认)
                'bPaginate' : false, // 是否分页
                "bProcessing" : false, // 当datatable获取数据时候是否显示正在处理提示信息。
                "bServerSide" : false, // 指定服务器端分页
                "bDeferRender" : false,
                "bFilter" : true,
                "bInfo" : false,
                "bSort" : true,
                "sAjaxSource" : param.ajaxUrl, // 数据源
                "aoColumns" : [
                {
                    "mData" : "menuorder",
                    "bVisible" : false,
                },
                {
                    "sTitle" : "菜单名称",
                    "bSortable" : true,
                    "sWidth" : "25%",
                    "sClass" : "",
                    "mData" : "name",
                },
                {
                    "sTitle" : "菜单图标",
                    "bSortable" : true,
                    "mData" : "icon",
                },
                {
                    "sTitle" : "菜单链接",
                    "bSortable" : true,
                    "mData" : "url",
                },
                {
                    "sTitle" : "排序",
                    "bSortable" : true,
                    "mData" : "menuorder",
                },
                {
                    "sTitle" : "状态",
                    "bSortable" : true,
                    "mData" : "status",
                    "mRender" : function(data, type, full)
                    {
                        var ret = "";
                        if (0 == data)
                            ret = "启用";
                        if (1 == data)
                            ret = "禁用";
                        return ret;
                    }
                },
                {
                    "sTitle" : "操作",
                    "bSortable" : false,
                    "mData" : "id",
                    "mRender" : function(data, type, full)
                    {
                        return '<div class="btn-group" style="margin-top: 8px;"><a href="javascript:void(0);" class="btn icon-edit contrast" title="修改"></a><a href="javascript:void(0);" class="btn icon-trash contrast" title="删除"></a></div>';
                    }
                }]
            });

            $("#creatbtn").on("click", function(e)
            {
                $("#optForm").remove();
                var menuform = require("./menuform");
                menuform.init(param.divId, null);
            });
            $("#menutable tbody").on("click", "tr>td>div>a.icon-edit", function(e)
            {
                $("#optForm").remove();
                var rowNode = this.parentNode.parentNode.parentNode;
                var rowData = menutable.fnGetData(rowNode);
                var menuform = require("./menuform");
                menuform.init(param.divId, rowData);
            });
            $("#menutable tbody").on("click", "tr>td>div>a.icon-trash", function(e)
            {
                var rowNode = this.parentNode.parentNode.parentNode;
                var index = rowNode._DT_RowIndex;
                var rowData = menutable.fnGetData(rowNode);
                bootbox.confirm("确定删除吗？", function(YesOrNo)
                {
                    if (YesOrNo)//确定后逻辑
                    {
                        $.ajax(
                        {
                            url : GLOBAL.URL.MENU_DELETE + rowData.id + ".do",
                            dataType : "json",
                            type : "DELETE",
                            success : function()
                            {
                                menutable.fnDeleteRow(index);
                            }
                        });
                    }
                });
            });
        });
    };
});
